package com.atguigu.service_edu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.service_edu.entity.EduTeacher;
import com.atguigu.service_edu.entity.vo.EduTeacherQuery;
import com.atguigu.service_edu.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author clark
 * @since 2020-04-01
 */
@Api(description = "讲师管理")
@RestController
//@CrossOrigin//跨域
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    Logger log = LoggerFactory.getLogger(EduTeacherController.class);

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("list")
    public R list(){
        List<EduTeacher> list = eduTeacherService.list(null);

        return R.ok().data("items",list);

    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name="id",value="讲师ID",required=true)
            @PathVariable String id
    ){
        eduTeacherService.removeById(id);
        return R.ok();
    }



    @ApiOperation(value = "分页讲师列表")
    @PostMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page",value = "当前页码" , required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit",value = "每页记录数" ,required = true)
            @PathVariable Long limit,
            @ApiParam(name = "EduTeacherQuery",value="查询对象",required = false)
            @RequestBody
            EduTeacherQuery eduTeacherQuery
    ){
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        eduTeacherService.pageQuery(pageParam,eduTeacherQuery);

        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("save")
    public R save(
            @ApiParam(name = "EduTeacher" , value="讲师对象",required = true)
            @RequestBody
            EduTeacher eduTeacher
    ){

        eduTeacherService.save(eduTeacher);
        return R.ok();
    }

    @ApiOperation(value="根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id",value ="讲师ID",required = true)
            @PathVariable
            String id
    ){
        EduTeacher eduTeacher = eduTeacherService.getById(id);

        return R.ok().data("item",eduTeacher);
    }

    @ApiOperation(value="根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name="id",value="讲师ID",required = true)
            @PathVariable
            String id,
            @ApiParam(name="EduTeacher",value="讲师对象",required = true)
            @RequestBody
            EduTeacher eduTeacher
    ){
        eduTeacher.setId(id);
        eduTeacherService.updateById(eduTeacher);
        return R.ok();
    }



}

